package com.sigurolio.cruspath.ui;

import java.util.HashMap;
import java.util.Map;
import com.sigurolio.cruspath.R;
import com.sigurolio.cruspath.models.Controller;
import com.sigurolio.cruspath.models.Deity;
import com.sigurolio.cruspath.models.Player;
import com.sigurolio.cruspath.models.Race;
import com.sigurolio.cruspath.models.Team;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Lobby extends Activity{
	
	String onlineState;
	Map<Byte, Player> playerMap = new HashMap<Byte, Player>();
	byte player1Id = 1;
	byte player2Id = 2;
	byte player3Id = 3;
	byte player4Id = 4;
	/**
	 * onlineState: is "true" if the lobby is set for multiplayer mode, otherwise "false"
	 * playerMap is a map of all 4 players. It uses byte Player.playerId as key
	 */
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	final String[] controllerList = getResources().getStringArray(R.array.cruspath_ControllerList);
    	
    	/**
    	 * controller-, deity- and teamList are arrays/lists of items that players choose between in the playerSettings dialog
    	 * 
    	 */

    	//Create the view for this activity
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lobby);
        
        //Single Player or Multiplayer setup
        onlineState = getIntent().getExtras().getString("online"); 
        if(onlineState.equals("false"))
        	initSinglePlayerLobby();
        else
        	initMultiPlayerLobby();
        
        //Create players
        initPlayerSettings();
        
    }

	private void initSinglePlayerLobby() {
		TextView lobbyTitle = (TextView) findViewById(R.id.lobby_Title);
		lobbyTitle.setText(R.string.lobby_SinglePlayerTitle);
	}
	

	private void initMultiPlayerLobby() {
		TextView lobbyTitle = (TextView) findViewById(R.id.lobby_Title);
		lobbyTitle.setText(R.string.lobby_MultiplayerTitle);
	}
	
	private String getPlayer1Name() {
		//Use this method to get the name of the host/user from the android device (can also be changed in cruspath_Settings)
		return "Silli";
	}
	
	private void setPlayerSettingsButtons() {
		//Stilla alla takkana til að senda rétt Id í playerSettings(playerId)
	}
	
	private void initPlayerSettings() {
		//Player 1 is host (special settings)
		Player player1 = new Player(player1Id);
		player1.setDeity(Deity.CATHLON);
		player1.setRace(Race.CHRUSTNAR);
		player1.setTeam(Team.TEAM1);
		
		String player1Name = getPlayer1Name();
		player1.setName(player1Name);
		
		//Rest of players
		Player player2 = new Player(player2Id);
		player2.setName("player 2");
		player2.setDeity(Deity.SHIVEN);
		player2.setRace(Race.SLAMAR);
		player2.setTeam(Team.TEAM2);
		
		Player player3 = new Player(player3Id);
		player3.setName("player 3");
		player3.setDeity(Deity.PRATHON);
		player3.setRace(Race.CHRUSTNAR);
		player3.setTeam(Team.TEAM3);
		
		Player player4 = new Player(player4Id);
		player4.setName("player 4");
		player4.setDeity(Deity.SUNDEN);
		player4.setRace(Race.SLAMAR);
		player4.setTeam(Team.TEAM4);
		
		//All players are put in the map for access
		playerMap.put(player1Id, player1);
		playerMap.put(player2Id, player2);
		playerMap.put(player3Id, player3);
		playerMap.put(player4Id, player4);
	}
	
	private void setplayerSettings(byte playerId) {
		//Alert Dialog Content
		
		
		
		//Alert Dialog Container
		AlertDialog.Builder playerSettingsAlert = new AlertDialog.Builder(this);
		playerSettingsAlert.setMessage(R.string.lobby_PlayerSettingsTitle)
		       .setCancelable(true)
		       .setPositiveButton(R.string.cruspath_Accept, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   
		               Lobby.this.finish();
		           }
		       })
		       .setNegativeButton(R.string.cruspath_Deny, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		playerSettingsAlert.show();
	}
	
    public void startGame(View view)  {
        Intent gameIntent = new Intent(view.getContext(), Battlefield.class);
        startActivity(gameIntent);
    }
	
	
}
